//
//  CacheManangerViewController.m
//  SmartiOSPublisher
//
//  Created by Liu,Qingcan on 2017/5/1.
//  Copyright © 2017年 Baidu. All rights reserved.
//

#import "CacheManangerViewController.h"
#import "User_RoomAllInfoApi.h"
#import "UserModel.h"
#import "RoomModel.h"
#import "LEUserRoomItem.h"
#import "YYWebImage.h"

#import "VideoModel.h"

@interface CacheManangerViewController ()<UITableViewDelegate,UITableViewDataSource>


@property (nonatomic,strong)UITableView *myTableview;
//@property (nonatomic,strong)NSMutableArray *dataList;
@property (nonatomic,assign)NSInteger userId;

@property (nonatomic,strong)NSMutableArray *statusList;


@end

@implementation CacheManangerViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.myTableview];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self.navigationController setNavigationBarHidden:NO];
    self.tabBarController.tabBar.hidden = NO;
   
}




#pragma mark delegate
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    UIView *footer = [[UIView alloc] init];
//    //        footer.frame = CGRectMake(0, 0, self.view.bounds.size.width, 30);
//    footer.backgroundColor = [UIColor lightGrayColor];
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.bounds.size.width/2.f - 150, 10, 300, 30)];
//    label.text = @"-----没有更多了-----";
//    label.textAlignment = NSTextAlignmentCenter;
//    [footer addSubview:label];
//    return footer;
//}

//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 50;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"statusCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        
        
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"清除图片缓存";
         YYImageCache *cache = [YYWebImageManager sharedManager].cache;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"缓存内存:%.1f kb",(unsigned long)cache.diskCache.totalCost/1024.f];

    }else{
        cell.textLabel.text = @"清除数据库缓存";
    }
   
    
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *str = nil;
    if (indexPath.row == 0) {
        str = @"清除图片缓存成功";
         YYImageCache *cache = [YYWebImageManager sharedManager].cache;
        // 清空缓存
        [cache.memoryCache removeAllObjects];
        [cache.diskCache removeAllObjects];
        
        // 清空磁盘缓存，带进度回调
        [cache.diskCache removeAllObjectsWithProgressBlock:^(int removedCount, int totalCount) {
            // progress
        } endBlock:^(BOOL error) {
            // end
        }];

    }else{
        str = @"清除所有数据库缓存成功";
//        UserModel *user = [[UserModel alloc]init];
//        [user deleteAllUser];
        VideoModel *video = [[VideoModel alloc]init];
        [video deleteAllVideo];
        RoomModel *room = [[RoomModel alloc]init];
        [room deleteAllRoom];
    }
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"成功通知" message:str preferredStyle:UIAlertControllerStyleAlert];
    
    __weak typeof(self) wself = self;
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        //                        [wself popoverPresentationController];
//        [wself.navigationController popViewControllerAnimated:YES];
    }];
    [alertVC addAction:okAction];
    [self.myTableview reloadData];
    
    [self presentViewController:alertVC animated:YES completion:nil];
    
}



#pragma get

- (UITableView *)myTableview{
    if (!_myTableview) {
        _myTableview = [[UITableView alloc]initWithFrame:self.view.bounds];
        _myTableview.delegate = self;
        _myTableview.dataSource = self;
        
        [_myTableview setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
        _myTableview.backgroundColor = [UIColor whiteColor];
        _myTableview.showsVerticalScrollIndicator = YES;
        _myTableview.showsHorizontalScrollIndicator = NO;
        _myTableview.alwaysBounceVertical = YES;
        _myTableview.contentInset = UIEdgeInsetsMake(5, 3, 3, 3);
        
    }
    return _myTableview;
}


//
//- (NSMutableArray *)dataList{
//    if (!_dataList) {
//        _dataList = [NSMutableArray array];
//    }
//    return _dataList;
//}

- (NSMutableArray *)statusList{
    if (!_statusList) {
        _statusList = [NSMutableArray array];
    }
    return _statusList;
}



@end
